apiready = function(){
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service"],function(V,$api,$,$app,service){
	var vm = {
		el: '.container',
	  	data: {
	  		is_Show: false,
	  		activeIndex: 0,
	    	tabTxt:['未使用','已使用','已过期'],
	    	list: [],
	    	state: 0,
	    	page: 1,
	    	nullTxt: '你还没有可用的优惠券',
	    	have_data: true
	  	},
	  	methods:{
	  		init: function(isNull){
	  			if(this.have_data){
	  				this.getList(isNull);
	  			}else{
	  				$app.toast("没有更多数据了");
	  			}
	  		},
	  		listNull: function(listLength){
	  			if(listLength <= 0){
					this.$refs.myCouponNull.style.display = "block";
				}else{
					this.$refs.myCouponNull.style.display = "none";
				}
	  		},
	  		//--获取数据 state(0未使用，1已使用，2已过期)
	  		getList: function(isNull){
	  			var unionid = $api.getStorage("shop_unionid"),
	  				self = this, _state = isNull || false;
		  		$app.ajax(service.getMyCouponDiscountList,{
					data: {
						values: {
							unionid: unionid,
							state: self.state,
							page: self.page
						}
					}
				},function (res) {
					console.log(JSON.stringify(res));
					if(res.code == 0){
						var _data = res.data;
						if(_state){
							self.list = [];
						}
						if(res.data.length > 0){
							if(Array.isArray(_data)){
								for(var i=0; i < _data.length; i++){
									self.list.push(_data[i]);
								}
							}else{
								self.list.push(_data);
							}
						}
						self.page++;
						self.have_data = res.data.length <= 0 ? false : true;
						console.log(self.have_data);
					}
					self.listNull(self.list.length || 0);
				});
	  		},
	  		//--tab切换
	  		myCouponTab: function(et){
	  			var el = et.currentTarget;
	  			var index = el.dataset.index;
	  			this.activeIndex = +index;
	  			document.querySelector("#myCouponEdit").innerHTML = "编辑";
	  			this.is_Show = false;
	  			switch(+index){
	  				case 0:
	  					this.state = 0;
	  					this.page = 1;
	  					this.nullTxt = "你还没有可用的优惠券";
	  					this.getList(true);
	  					break;
	  				case 1:
	  					this.state = 1;
	  					this.page = 1;
	  					this.nullTxt = "你还没有已使用的优惠券！";
	  					this.getList(true);
	  					break;
	  				case 2:
	  					this.state = 2;
	  					this.page = 1;
	  					this.nullTxt = "你还没有已过期的优惠券！";
	  					this.getList(true);
	  					break;
	  				default:
	  					break;
	  			}
	  		},
	  		//--取消编辑
	  		cancelEdit: function(et){
	  			this.is_Show = false;
	  			document.querySelector("#myCouponEdit").innerHTML = "编辑";
	  		},
	  		//--全选
	  		myCouponCheckAll: function(et){
	  			var list = document.querySelectorAll("#myCouponList input");
	  			if(et.target.checked){
	  				for(var i=0; i <list.length; i++){
	  					list[i].checked = true;
	  				}
	  			}else{
	  				for(var i=0; i <list.length; i++){
	  					list[i].checked = false;
	  				}
	  			}
	  		},
	  		//--选择
	  		listInputCheck: function(et){
	  			var list = document.querySelectorAll("#myCouponList input");
	  			for(var i =0; i <list.length; i++){
	  				if(!list[i].checked){
	  					document.querySelector("#myCouponCheckAll").checked = false;
	  					break;
	  				}
	  				if(list[i].checked && i == list.length-1){
	  					document.querySelector("#myCouponCheckAll").checked = true;
	  				}
	  			}
	  		},
	  		//--删除优惠券
	  		couponDel: function(et){
	  			var unionid = $api.getStorage("shop_unionid"), self = this;
	  			var coupon_get_id = '', _id = [],delIndex = [];
	  			var li = this.$refs.myCouponList.querySelectorAll("input[type='checkbox']");
	  			for(var i=0; i < li.length; i++){
	  				if(li[i].checked){
	  					_id.push(li[i].dataset.cdid)
	  					delIndex.push(+li[i].dataset.index);
	  				}
	  			}
	  			coupon_get_id = _id.join(",");
	  			$app.ajax(service.delCouponUserGet,{
					data: {
						values: {
							unionid: unionid,
							coupon_get_id: coupon_get_id
						}
					}
				},function (res) {
					console.log(JSON.stringify(res));
					if(res.code == 0){
						var li = self.$refs.myCouponList.querySelectorAll("input");
			  			for(var i=0; i < li.length; i++){
			  				li[i].checked = false;
			  			}
						self.$refs.myCouponCheckAll.checked = false;
						self.is_Show = true;
						self.have_data = true;
						self.page = 1;
						self.init(true);
						api.toast({
						    msg: '优惠券删除成功',
						    duration: 2000,
						    location: 'middle'
						});
					}
				});
	  		},
	  		//--使用优惠券
	  		goShopMain: function(et){
	  			if(this.state != 0){
	  				return;
	  			}
	  			var unionId = $api.getStorage("shop_unionid");
            	var user_id = et.currentTarget.dataset.userid;
            	$app.ajax(service.getOtherShop, {
                    data: {
                        values: {
                            unionid: unionId,
                            other_user_id: user_id
                        }
                    }
               },function(res) {
                    //console.log(JSON.stringify(res));
                    if (res.code == 0){
                    	$app.openWin('buyMain',{
                    		shopData: res.data,
                    		otherUserInfoId: user_id
                    	});
                    }
                });
	  		}
	  	},
	  	mounted:function() {
	  		this.init();
	  		var self = this;
	  		//--编辑
	  		document.querySelector("#myCouponEdit").addEventListener("click",function(){
	  			if(!self.is_Show){
	  				this.innerHTML = '完成';
	  				self.is_Show = true;
	  			}else{
	  				this.innerHTML = '编辑';
	  				self.is_Show = false;
	  			}
	  		});
	  		api.addEventListener({
				name: 'scrolltobottom',
				extra:{
					threshold: 0            //设置距离底部多少距离时触发，默认值为0，数字类型
				}
			}, function(ret, err){
				self.init();
			});
	  	},
	  	created: function(){

	  	}
	};
	var even = function() {}
	var newVue = new Vue(vm);
	even();
});
}
